function testPassword(passwd) {
    var intScore = 0
    var strVerdict = "weak"
    var strLog = ""

    // PASSWORD LENGTH
    if (passwd.length < 5)                         // length 4 or less
    {
        intScore = (intScore + 3)
        strLog = strLog + "3 points for length (" + passwd.length + ")\n"
    }
    else if (passwd.length > 4 && passwd.length < 8) // length between 5 and 7
    {
        intScore = (intScore + 6)
        strLog = strLog + "6 points for length (" + passwd.length + ")\n"
    }
    else if (passwd.length > 7 && passwd.length < 16)// length between 8 and 15
    {
        intScore = (intScore + 12)
        strLog = strLog + "12 points for length (" + passwd.length + ")\n"
    }
    else if (passwd.length > 15)                    // length 16 or more
    {
        intScore = (intScore + 18)
        strLog = strLog + "18 point for length (" + passwd.length + ")\n"
    }


    // LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
    if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
    {
        intScore = (intScore + 1)
        strLog = strLog + "1 point for at least one lower case char\n"
    }

    if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
    {
        intScore = (intScore + 5)
        strLog = strLog + "5 points for at least one upper case char\n"
    }

    // NUMBERS
    if (passwd.match(/\d+/))                                 // [verified] at least one number
    {
        intScore = (intScore + 5)
        strLog = strLog + "5 points for at least one number\n"
    }

    if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
    {
        intScore = (intScore + 5)
        strLog = strLog + "5 points for at least three numbers\n"
    }


    // SPECIAL CHAR
    if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
    {
        intScore = (intScore + 5)
        strLog = strLog + "5 points for at least one special char\n"
    }

    // [verified] at least two special characters
    if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {
        intScore = (intScore + 5)
        strLog = strLog + "5 points for at least two special chars\n"
    }


    // COMBOS
    if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
    {
        intScore = (intScore + 2)
        strLog = strLog + "2 combo points for upper and lower letters\n"
    }

    if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
    {
        intScore = (intScore + 2)
        strLog = strLog + "2 combo points for letters and numbers\n"
    }

    // [verified] letters, numbers, and special characters
    if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {
        intScore = (intScore + 2)
        strLog = strLog + "2 combo points for letters, numbers and special chars\n"
    }

    return intScore;

}

// ------------------------------------------------------------------------------------------------


var scoreSkala = [16, 35];
var verdicts = ["Αδύναμο", "Καλό", "Πολύ καλό"];
var verdictsImg = ["psVeryWeak", "psWeak", "psStrong"];

var skalaLength = 33;

// ------------------------------------------------------------------------------------------------

function checkPassword(element){
				//var element = document.getElementById("passwdstrengthText");
				//var scoreElement = document.getElementById("passwdstrengthScore");
    var score = testPassword (element.value);

	var verdict = "";
	if (score<scoreSkala[0]){
		verdict = verdicts[0];
	}else if(score >= scoreSkala[0] && score < scoreSkala[1]){
		verdict = verdicts[1];
	}else{
		verdict = verdicts[2];
	}
				
	displayStrength(score, true);
				
				//element.innerHTML = verdict;
}
	
//------------------------------------------------------		
		
function displayStrength(score, clear) {
    if (clear) {
        clearStrength();
    }

    if (score < Number(scoreSkala[0])) {
        var x = Math.floor((Number(score) * Number(skalaLength)) / Number(Number(scoreSkala[0]) - 1));

        if (x <= 6) {
            x = 0;
        }
        document.getElementById(verdictsImg[0]).innerHTML = verdicts[0];
        document.getElementById(verdictsImg[0]).style.width = x + "%";
        
        //}
    }

    if (score >= Number(scoreSkala[0]) && score < Number(scoreSkala[1])) {
        var x = Math.floor((Number(score) * Number(skalaLength)) / Number(Number(scoreSkala[1]) - 1));

        document.getElementById(verdictsImg[1]).innerHTML = verdicts[1];
        document.getElementById(verdictsImg[1]).style.width = x + "%";

        displayStrength(Number(scoreSkala[0]) - 1, false);
    }


    if (score >= scoreSkala[1]) {
        var x = Math.floor(((Number(score) * Number(skalaLength)) / 50));

        //for (var i = 0; i < x; i++) {

            //var img = new Image();
            //var id = i + (2 * skalaLength);
            //img.id = "ps" + id;
            //img.src = contextPath + "/img/" + verdictsImg[2];
            //document.getElementById("ps" + id).src = img.src;

        //document.getElementById(verdictsImg[2]).innerHTML = x;
        document.getElementById(verdictsImg[2]).innerHTML = verdicts[2];
        document.getElementById(verdictsImg[2]).style.width = x + "%";

        //}

        displayStrength(Number(scoreSkala[1]) - 1, false);

    }
}

//------------------------------------------------------------------------------------------------------------

function clearStrength() {
    for (var i = 0; i < verdictsImg.length; i++) {
        document.getElementById(verdictsImg[i]).style.width = "0%";
        document.getElementById(verdictsImg[i]).innerHTML = "";
    }
}

//------------------------------------------------------------------------------------------------------------

function clearStrengthImage(){
	for (var i=0; i< skalaLength * verdicts.length; i++){
		var img = new Image();
		img.id = "ps" + String(i);
		img.src = contextPath + "/img/b_gray.png";
		document.getElementById("ps" + i).src = img.src;
	}
}

//------------------------------------------------------

function displayStrengthImage(score, clear){
	if (clear){
		clearStrengthImage();
	}
	
	if(score < Number(scoreSkala[0])){
		var x = Math.floor((Number(score) * Number(skalaLength)) / Number(Number(scoreSkala[0])-1));

		for (var i=0; i<x; i++){
			var img = new Image();
			var id = i;
			img.id = "ps" + i;
			img.src = contextPath + "/img/" + verdictsImg[0];
			document.getElementById("ps" + id).src = img.src;
		}		
	}
	
	if (score >= Number(scoreSkala[0]) && score < Number(scoreSkala[1])){
		var x = Math.floor((Number(score) * Number(skalaLength)) / Number(Number(scoreSkala[1])-1));
		
		for (var i=0; i<x; i++){
			var img = new Image();
			var id = i + skalaLength;
			img.id = "ps" + id;
			img.src = contextPath + "/img/" + verdictsImg[1];
			document.getElementById("ps" + id).src = img.src;
		}
		displayStrengthImage(Number(scoreSkala[0])-1, false);
	}
	
	
	if (score >= scoreSkala[1]){
		var x = Math.floor(((Number(score) * Number(skalaLength)) / 50));
		
		for (var i=0; i<x; i++){
			var img = new Image();
			var id = i + (2*skalaLength);
			img.id = "ps" + id;
			img.src = contextPath + "/img/" + verdictsImg[2];
			document.getElementById("ps" + id).src = img.src;
		}
		displayStrengthImage(Number(scoreSkala[1])-1, false);
		
	}
}